<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;
use App\Models\Comment;
use App\Models\Topic;
use App\Models\Message;
use App\Models\User;
use Auth;
use App\Models\SubtopicModerator;
use App\Models\SubtopicSubscriber;

use App\Http\Requests\NewPostRequest;
use App\Http\Requests\NewCommentRequest;
use App\Http\Requests\DeleteCommentRequest;

class PostController extends Controller
{
	public function showPost(string $sub,string $post_id){
		$post = Post::where('subwroteit',$sub)->where('post_id_string',$post_id)->first();
		$comments = Comment::where('subwroteitID',$sub)->where('postID',$post_id)->orderby('updated_at','desc')->paginate(10);
		$not_moderator = false;
		if(Auth::check()){
            $isModerator = SubtopicModerator::where('moderator_id','=',Auth::user()->id)->where('subtopic',$sub)->first();

            if(is_null($isModerator)){
                $not_moderator = true;
            }
        }
        return view('wroteit.show_post')->with(['post' => $post])->with(['comments' => $comments])->with(['not_moderator' => $not_moderator]);
	}
	public function subscribe(string $sub){
		$userid=Auth::user()->id;
        $rel = array('subtopic' => $sub, 'subscriber_id' => $userid);
        SubtopicSubscriber::create($rel);
		Topic::where('title', '=', $sub)->increment('subscriptions');
        return redirect()->route('show_topic',['sub' => $sub]);
	}

	public function unsubscribe(string $sub){
		$userid=Auth::user()->id;
		SubtopicSubscriber::where('subtopic',$sub)->where('subscriber_id',$userid)->delete();
		Topic::where('title', '=', $sub)->decrement('subscriptions');
		return redirect()->route('show_topic',['sub'=>$sub]);
	}

	public function showTopics(){
		$topics = Topic::orderby('updated_at','desc')->get();
        return view('wroteit.list_topics')->with(['topics' => $topics]);
	}

	public function showSubscriptions(){
		$subbed = SubtopicSubscriber::select('subtopic')->where('subscriber_id',Auth::user()->id);
    	$topics = Topic::orderby('updated_at','desc')->whereIn('title',$subbed)->paginate(6);
        return view('wroteit.list_topics')->with(['topics' => $topics]);
	}
	public function newPost(string $sub){
		return view('wroteit.new_post')->with(['sub' => $sub]);
	}
    public function storeNewPost(NewPostRequest $request){
		$post = new Post;
		$post->title = $request->input('title');
		$post->content = $request->input('content');
        $post->by_user = Auth::user()->name;
		$post->subwroteit = $request->input('sub');
		$post->post_id_string = $this->generateFancyPostID($request->input('sub'),$request->input('title'));
		$post->score = 0;
		$post->number_comments = 0;
		$post->save();
		Topic::where('title','=',$request->input('sub'))->increment('posts_number');
		// get subwroteit and increment field posts_number by 1
        //$this->store_relation_subtopicSubscriber($subtopic_name, $user_id);
        //return redirect()->route('show_post',['sub' => $request->input('sub')],['post_id' => $post->post_id_string]);
		return redirect()->route('show_post',['sub' => $request->input('sub'),'post_id' => $post->post_id_string]);
    }

	public function storeNewComment(NewCommentRequest $request){
		$comment = new Comment;
		$comment->content = $request->input('content');
        $comment->userName = Auth::user()->name;
		$comment->subwroteitID = $request->input('sub');
		$comment->postID = $request->input('postID');
		$comment->save();
		Post::where('subwroteit',$request->input('sub'))->where('post_id_string',$request->input('postID'))->increment('number_comments');

		return redirect()->route('show_post',['sub' => $request->input('sub'),'post_id' => $request->input('postID')]);
	}

	public function deleteComment(DeleteCommentRequest $request){
		$id = $request->input('id');
		$subwroteID = $request->input('subwroteID');
		$postID = $request->input('postID');
		Comment::where('id',$request->input('id'))->delete();
		Post::where('subwroteit',$subwroteID)->where('post_id_string',$postID)->decrement('number_comments');

		return redirect()->route('show_post',['sub' => $subwroteID,'post_id' => $postID]);
	}

	/*
	 *public static function generateFancyPostID(string $sub,string $title){
	 *    $vanilla=$title . "-" . date("y-m-d-");
	 *    $foundFancy=false;
	 *    $number=0;
	 *    while(!$foundFancy){
	 *        $number++;
	 *        $fancy=$vanilla . $number;
	 *        $post = Post::where('subwroteit',$sub)->where('post_id_string',$fancy)->first();
	 *        if(is_null($post)){
	 *            $foundFancy=true;
	 *        }
	 *    }
	 *    $fancy=str_replace(" ","-",$title);
	 *    return $fancy;
	 *}
	 */
	public static function generateFancyPostID(string $sub,string $title){
        $title = $title . "-" . date("y-m-d-");
	    $title=str_replace(" ","-",$title);
        $foundFancy=false;
        $number=0;
        while(!$foundFancy){
            $number++;
            $fancy=$title . $number;
            $post = Post::where('subwroteit',$sub)->where('post_id_string',$fancy)->first();
            if(is_null($post)){
                $foundFancy=true;
            }
        }
        return $fancy;
    }
}
